<?php
/**
 * @user:andy
 * @date:2021/8/4
 * @company:http://www.sjdphp.com
 * @desc:
 */

namespace app\admin\controller;


use app\common\controller\BackBaseController;
use app\common\server\Json;
use app\common\model\AdCate as AdCateModel;
use app\common\server\SystemLog;
class Ad extends BackBaseController
{
    protected $model_id = 'ad_id';
    protected $model_name = 'ad_name';
    protected $model_cate = 'cate_id';
    protected $model_status = 'ad_status';
    protected $model_desc = 'ad_sort desc';

    public function lists(){
        ## 收参
        $page = $this->request->param("page",0,'intval');
        $limit = $this->request->param("limit",0,'intval');
        $searchParams = json_decode($this->request->param("searchParams"),true);

        ## 默认查询数据的状态
        $term[] = [$this->model_status,'<>',config("project.mysql_code.table_delete")];

        ## 判断是否存在搜索数据
        if(!empty($searchParams)){
            if(isset($searchParams['name']) && $searchParams['name'] !=''){
                $term[] = [$this->model_name,'like','%'.$searchParams['name'].'%'];
            }
            if(isset($searchParams["cate_id"]) && is_numeric($searchParams['cate_id'])){
                $term[] = [$this->model_cate,'=',$searchParams['cate_id']];
            }
        }

        ## 获取实例
        $class = self::getBusExample($this->request->controller());
        $res = $class::getLists($term,'',$page,$limit,$this->model_desc);

        ## 判断请求方式并返回数据
        if($this->request->isPost()){
            return $res;
        }

        return view('',[
            'cates' => $this->getAdCateLists()
        ]);
    }

    public function add(){
        if($this->request->isPost()){
            ## 接受参数
            $param = $this->request->param();

            ## 获取验证层实例对象
            $validate = self::getBusExample($this->request->controller(),2);
            if(!$validate->scene("add")->check($param)) return Json::error($validate->getError());

            ## 获取默认的状态
            $param[$this->model_status] = config("project.mysql_code.table_pending");
            ## 获取操作人
            $param["operator"] = $this->request->BackInfo["back_name"];

            ## 调用业务层处理
            $class = self::getBusExample($this->request->controller());
            $res = $class::insertOrUpdateAndDel($param);

            return $res;
        }else{
            return view('',[
                'cates' => $this->getAdCateLists()
            ]);
        }
    }

    public function edit(){
        if($this->request->isPost()){
            ## 接受参数
            $param = $this->request->param();

            ## 获取验证层实例对象
            $validate = self::getBusExample($this->request->controller(),2);
            if(!$validate->scene("edit")->check($param)) return Json::error($validate->getError());

            ## 获取操作人
            $param["operator"] = $this->request->BackInfo["back_name"];

            ## 调用业务层处理
            $class = self::getBusExample($this->request->controller());
            $res = $class::insertOrUpdateAndDel($param,2);

            return $res;
        }else{
            $ad_id = $this->request->param("ad_id",0,'intval');
            if(!empty($ad_id)){
                $class = self::getBusExample($this->request->controller());
                $info = $class::getInfo(array($this->model_id => $ad_id));
            }

            return view('',[
                'info'  => $info,
                'cates' => $this->getAdCateLists(),
            ]);
        }
    }



    protected function getAdCateLists(){
        try{
            ## 获取广告位置数据
            $cateLists = AdCateModel::where(array("cate_status" => config("project.mysql_code.table_normal")))->field("cate_id,cate_name")->select();
        }catch(\Exception $e){
            SystemLog::insertLog($e->getMessage());
        }

        return $cateLists;
    }

}